<?php
/*
 * core/util.php
 *
 * Toolbox for various tasks
 *
 */

class Util
{
	/* 
	 * getUrlScriptName()
	 * return the full url to the currently executed page
	 */
	public static function getUrlScriptName()
	{
		$scr = (isset($_SERVER['HTTPS']))?'https':'http' . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
		return $scr;
	}

	/*
	 * getPregLastErrorString()
	 * return last PREG error as string
	 */
	public static function getPregLastErrorString()
	{
		switch( preg_last_error() )
		{
			case PREG_INTERNAL_ERROR: 				return 'Internal error'; break;
			case PREG_BACKTRACK_LIMIT_ERROR: 	return 'Backtrack limit error'; break;
			case PREG_RECURSION_LIMIT_ERROR: 	return 'Recursion limit error'; break;
			case PREG_BAD_UTF8_ERROR: 				return 'Bad UTF8 error'; break;
			case PREG_BAD_UTF8_OFFSET_ERROR:	return 'Bad UTF8 offset error'; break;
		}
		return 'Unknown error';
	}

	/*
	 * getBacktrace()
	 * return current backtrace (including the call to getBacktrace() itself),
	 * and "handling" when called through call_user_func_array() (it's missing file and line)
	 * return array( array('class'=>, 'mth'=>, 'file'=>, 'line'=>), ... )
	 */
	public static function getBacktrace()
	{
		$bt = debug_backtrace( $provide_object=false );

		$record = array( 'class'=>null, 'mth'=>null, 'file'=>null, 'line'=>null );
		$out = array();
		$i = 0;
		$count = sizeof($bt);
		while( $count-- )
		{
			$b =& $bt[$count];

			// we keep the previous file and line in case it's missing
			$record['class'] = (isset($b['class'])) ? $b['class'] : '';
			$record['mth'] = (isset($b['function'])) ? $b['function'] : '';
			if( isset($b['file']) ) $record['file'] = $b['file'];
			if( isset($b['line']) ) $record['line'] = $b['line'];

			$out[$i++] = $record;
		}
		
		return $out;
	}

};

?>
